package com.bawei.mvvmdemo.databindingdemo;

import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;

import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

import com.bawei.mvvmdemo.R;
import com.bawei.mvvmdemo.databinding.ActivityDemoBinding;

public class DemoActivity extends AppCompatActivity {
    private ActivityDemoBinding mActivityDemoBinding;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mActivityDemoBinding = DataBindingUtil.setContentView(this,R.layout.activity_demo);
        //设置数据源
        User user = new User("李绍元", 19);
        user.setShow(true);
        user.setImageUrl("https://profile-avatar.csdnimg.cn/b6efd2e74a594207a2732ffa918e5d19_qq_34178710.jpg!1");
        mActivityDemoBinding.setUser(user);
        //设置事件监听
        OnClickHandler onClickHandler = new OnClickHandler();
        mActivityDemoBinding.setOnclickHandler(onClickHandler);
        //省略findViewByID
        mActivityDemoBinding.iv.setImageResource(R.mipmap.ic_launcher);
        mActivityDemoBinding.bt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                user.setUsername("李大明白");
                user.setAge(100);
                mActivityDemoBinding.setUser(user);
                Toast.makeText(DemoActivity.this,"点击了按钮",Toast.LENGTH_LONG).show();
            }
        });

    }
}